package com.lovo.pis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.pis.constant.MessageCode;
import com.lovo.pis.dto.BuyCarGoodsBean;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.service.IBuyCarGoodsService;
import com.lovo.pis.utils.FileUtil;
import com.lovo.pis.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/buyCarGoods")
public class BuyCarGoodsController {

    @Autowired
    IBuyCarGoodsService buyCarGoodsService;

    //根据购物车id分页查询所有
    @RequestMapping("/queryAll")
    public IPage<BuyCarGoodsBean> queryAllBuyCarGoods(@RequestBody PageBean<BuyCarGoodsBean> pageInfo){
       return buyCarGoodsService.queryAllBuyCarGoods(pageInfo);
    }

    //根据购物车id和商品id修改商品单价和数量
    @RequestMapping("/change")
    public JsonResult changeItem(int carId,int id,int number,int price){
        JsonResult jsonResult = new JsonResult<>();
        buyCarGoodsService.changeItem(carId,id,number,price);
        return jsonResult;
    }

    //图片上传
    @RequestMapping("/uploadImg")
    public String uploadImg(MultipartFile file){
        return FileUtil.getFileName(file);
    }

    //移除商品
    @RequestMapping("/remove")
    public JsonResult removeById(int carId,int id){
        JsonResult jsonResult = new JsonResult<>();
        buyCarGoodsService.removeById(carId,id);
        return jsonResult;
    }

    //添加商品
    @RequestMapping("/add")
    public JsonResult addBuyCarGoods(@RequestBody BuyCarGoodsBean buyCarGoodsBean){
        JsonResult jsonResult = new JsonResult<>();
        buyCarGoodsService.addBuyCarGoods(buyCarGoodsBean);
        return jsonResult;
    }


}
